package us.versus.them.b3ge.phyxside;

import flash.net.URLRequest;

import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.Sprite;

/**
 *
 * Flipper is a registry of Worldly objects 
 * mapped by strings.
 *
 * A key and Wordly are passed to add. The 
 * activate message takes a key and flips
 * to the appropriate Wordly.
 *
 */
class Flipper {
	var flip : Sprite;
	var current : Worldly;
	var flops : Hash< Worldly >;

	public static function main() {
		new Flipper();
	}

	/**
	 *
	 * create a new Flipper
	 *
	 */
	public function new( ?flip:Sprite ) {
		if ( null == flip ) {
			this.flip = flash.Lib.current;
		} else {
			this.flip = flip;
		}
		current = null;
		flops = new Hash< Worldly >();

		this.add( Welcome.FLIP, new Welcome( this ) );
		this.add( App.FLIP, new App( this ) );

		// this.add( 'inventory', new Inventory( this ) );
		// this.add( 'character', new Character( this ) );

		activate( Welcome.FLIP );
	}

	/**
	 *
	 * add a Worldly with a given key
	 *
	 */
	public function add( key:String, worldly:Worldly ) {
		flops.set( key, worldly );
	}

	/**
	 *
	 * flip to the Worldly that matches the key
	 *
	 */
	public function activate( key:String ) {
		var next = flops.get( key );
		if ( null == next ) {
			trace( key + ' -> ' + next );
		} else {
			if ( null != current ) {
				flip.removeChild( current );
				current.deactivate();
			}
			current = next;
			flip.addChild( current );
			current.activate();
		}
	}
}
